package com.hmyd.hmyd_android.ui;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.commons.Constants;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.crypto.Crypto;
import com.hmyd.hmyd_android.dto.User;
import com.hmyd.hmyd_android.dto.VerifyCode;
import com.hmyd.hmyd_android.utils.MyToast;
import com.hmyd.hmyd_android.utils.PreferencesUtils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.shangzhu.apptrack.AppTrack_2423;

/**
 * 关闭账号保护
 * 
 * @author wangc
 * 
 */

@ContentView(R.layout.activity_close_device_protect)
public class CloseDeviceProtectActivity extends BasicActivity {
	// 标题栏
	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;

	@ViewInject(R.id.verification_code)
	private Button verification_code;

	@ViewInject(R.id.verification_edit)
	private EditText verification_edit;

	private String mCodeId;

	private final int SEND_REGISTER_VERIFY_CODE = 0;
	private final int SUBMMIT = 1;

	// 获取验证码
	@OnClick({ R.id.verification_code, R.id.btn_login })
	public void onClick(View view) {
		RequestParams params = new RequestParams();
		switch (view.getId()) {
		case R.id.verification_code:
			params.addBodyParameter("mobile", PreferencesUtils.getString(this, PreferencesUtils.USER_PHONE));
			onRequest(SEND_REGISTER_VERIFY_CODE, UrlConstant.SAFECODE, HttpMethod.POST, params);
			break;
		case R.id.btn_login:
			if (TextUtils.isEmpty(verification_edit.getText().toString().trim())) {
				MyToast.showHMToast(this, false, "请输入验证码");
				return;
			}
			params.addBodyParameter("flag", "false");
			params.addBodyParameter("codeId", mCodeId);
			params.addBodyParameter("verifyCode", verification_edit.getText().toString().trim());

			onRequest(SUBMMIT, UrlConstant.SWITCHDEVICECHAIN, HttpMethod.POST, params);

			break;

		default:
			break;
		}

	}

	@Override
	public void onPostSuccess(int postId, String result) {

		Gson gson = new Gson();
		switch (postId) {
		case SEND_REGISTER_VERIFY_CODE:
			handler.postDelayed(runnable, 0);
			java.lang.reflect.Type type = new TypeToken<VerifyCode>() {
			}.getType();
			VerifyCode verifyCode = gson.fromJson(result, type);
			mCodeId = verifyCode.codeId;

			break;
		case SUBMMIT:
			JSONObject jsonObject;
			try {
				jsonObject = new JSONObject(result);
				MyToast.showHMToast(this, true, jsonObject.getString("info"));
			} catch (JSONException e) {
				e.printStackTrace();
			}
			AppBasic.getInstance().finishCurrentActivity();

			break;

		default:
			break;
		}

	}

	@Override
	public void onPostFailure(int postId, String msg) {

	}

	@Override
	protected void setParams() {
		home_title.setOnTouchListener(touch);
		home_title_txt.setText("解除账号保护");

	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {

	}

	private int second = 60;
	Handler handler = new Handler();
	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			if (second >= 0) {
				verification_code.setText(second-- + "s");
				verification_code.setEnabled(false);
			} else {
				verification_code.setText(R.string.register_get_code);
				verification_code.setEnabled(true);

				second = 60;

				Message message = new Message();
				message.what = 1;
				handlerStop.sendMessage(message);
			}

			handler.postDelayed(this, 1000);
		}
	};

	final Handler handlerStop = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				handler.removeCallbacks(runnable);
				break;
			}
			super.handleMessage(msg);
		}
	};

}
